package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BaseBookMapper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.queryBean.BaseBookQueryBean;
import com.koron.css2.baseConfig.vo.BaseBookListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.SelectBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

public class MeterReadingStaffList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		
				@SuppressWarnings("rawtypes")
				MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
				try {
					BaseBookQueryBean bean = JsonUtils.objectToPojo(req.getData(), BaseBookQueryBean.class);
					BaseBookMapper mapper = factory.getMapper(BaseBookMapper.class);
					//营业区域单独处理
					if (bean.getBusinessArea() !=null && !"".equals(bean.getBusinessArea())){
						BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
						List<String> areaIdList = this.queryChildrenArea(bean.getBusinessArea(),userInfo.getCurWaterCode(),businessAreaMapper);
						bean.setBusinessAreaList(areaIdList);
					}
					//PageHelper.startPage(bean.getPage(), bean.getPageCount());
					List<BaseBookListVO> list = mapper.selectMeterReadingStaffName(bean);
					/*
					//营业所转换
					BusinessAbodeMapper abodemapper = factory.getMapper(BusinessAbodeMapper.class,"_default");
					// 获取营业区域
					
					//获取下拉框
					List<SelectVO> Abodes = abodemapper.businessAbodeSelect(userInfo.getCurWaterCode());
					Map<String, String> RTP = Tools.mapDicByCode(factory, "RTP");
					for (BaseBookListVO vo : list) {
						for (SelectVO abode : Abodes) {
							if(abode.getId().equals(vo.getBusinessAbode())) {
								vo.setBusinessAbodeName(abode.getName());
							}
						}
						vo.setMeterReadingCycleName(RTP.get(vo.getMeterReadingCycle()));
						if("1".equals(vo.getStatus())) {
							vo.setStatusName("启用");
						}else {
							vo.setStatusName("停用");
						}
						if (!StringUtils.isEmpty(vo.getBusinessArea())) {
							BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
							vo.setBusinessAreaName(
									businessAreaMapper.findBusinessAreaById(vo.getBusinessArea(), userInfo.getCurWaterCode()));
						}
					}*/
					
					info.setData(list);
				} catch (Exception e) {
					info.setCode(Constant.MESSAGE_INT_FAIL);
					info.setDescription("操作失败");
					logger.error("操作失败", e);
				}
				return info;
	}
	private List<String> queryChildrenArea(String parentId,String groupCode,BusinessAreaMapper mapper){
		List<SelectBean> list = mapper.findBusinessAreaByParentId(parentId,groupCode);
		List<String> result = new ArrayList<>();
		if (list != null && list.size()>0){
			for (SelectBean selectBean : list){
				List<String> sub = queryChildrenArea(selectBean.getId(),groupCode,mapper);
				result.addAll(sub);
			}
		}else {
			result.add(parentId);
		}
		//result.addAll(list.stream().map(SelectBean::getCode).collect(Collectors.toList()));

		return result;
	}
}
